local battleRoyalVs = fk.CreateSkill{
  name = "#battle_royal_peach&",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["#battle_royal_peach&"] = "鏖战",
  [":#battle_royal_peach&"] = "",
}

local cardSubtypeStrings = {
  [Card.SubtypeNone] = "none",
  [Card.SubtypeDelayedTrick] = "delayed_trick",
  [Card.SubtypeWeapon] = "weapon",
  [Card.SubtypeArmor] = "armor",
  [Card.SubtypeDefensiveRide] = "defensive_ride",
  [Card.SubtypeOffensiveRide] = "offensive_ride",
  [Card.SubtypeTreasure] = "treasure",
}

battleRoyalVs:addEffect("filter", {
  mute = true,
  card_filter = function (self, cardp, player, isJudgeEvent)
    --if player:getMark("_heg__battleRoyalVsMode_ignore") ~= 0 or not player:hasSkill("#battle_royal&", false, true) then return end

    if ClientInstance then
      --覆写2
      ---@param id integer
      ---@param filterCard? boolean @ 是否获取经过锁视的牌？
      function GetCardData(id, filterCard)
        local card = Fk:getCardById(id, not filterCard)
        if card == nil then
          return {
            cid = id,
            known = false
          }
        end
        local mark = {}
        for k, v in pairs(card.mark) do
          if k and k:startsWith("@") and v and v ~= 0 then
            table.insert(mark, {
              k = k, v = v,
            })
          end
        end
        local ret = {
          cid = id,
          name = card.name,
          extension = card.package.extensionName,
          number = card.number,
          suit = card:getSuitString(),
          color = card:getColorString(),
          mark = mark,
          type = card.type,
          subtype = cardSubtypeStrings[card.sub_type],
          multiple_targets = card.multiple_targets,
        }
        if filterCard and card.skillName ~= "" then
          local orig = Fk:getCardById(id, true)
          ret.name = orig.name
          ret.virt_name = card.name
        end
        if Self and card.trueName == "peach" and card.suit ~= Card.NoSuit and Fk:currentRoom():getCardArea(id) ~= Card.Unknown
          and Fk:currentRoom():getBanner("@[:]BattleRoyalDummy")
        then
          ret.extension = "standard_sifu_heg_peach"
        end
        return ret
      end
    end

    --if not cardp or Fk:getCardById(cardp.id, true).trueName ~= "peach" then return false end
    --if isJudgeEvent or not table.contains(player:getHandlyIds(), card.id) then return false end
    return false
  end,
  view_as = function (self, player, card)
    local c = Fk:cloneCard(card.name, card.suit, card.number)
    c.package = Fk.packages["standard_sifu_heg_peach"]
    return c
  end,
})

local function refresh(p)
  if table.find(p:getCardIds("h"), function (cid)
    return Fk:getCardById(cid, true).trueName == "peach"
  end) then
    --local room = p.room
    --local cards = room:syncPlayerClientCards(p)
    p._splayer:doNotify("ChangeSelf", cbor.encode(p._splayer:getId()))
    for _, s in ipairs(p._fake_skills) do
      p:doNotify("LoseSkill", { p.id, s.name, false })
      p:doNotify("AddSkill", { p.id, s.name, true })
      if table.contains(p.prelighted_skills, s) then
        p:doNotify("PrelightSkill", { s.name, true })
      end
    end
  end
end

battleRoyalVs:addEffect(fk.EventAcquireSkill, {
  can_refresh = function (self, event, target, player, data)
    return player == target and data.skill.name == "#battle_royal&"
  end,
  on_refresh = function (self, event, target, player, data)
    player:filterHandcards()
    refresh(player)
  end,
})

battleRoyalVs:addEffect(fk.EventLoseSkill, {
  can_refresh = function (self, event, target, player, data)
    return player == target and data.skill.name == "#battle_royal&"
  end,
  on_refresh = function (self, event, target, player, data)
    refresh(player)
  end,
})

battleRoyalVs:addAcquireEffect(function (self, player, is_start)
  player:filterHandcards()
  refresh(player)
end)

battleRoyalVs:addEffect(fk.GameStart, {
  can_refresh = function (self, event, target, player, data)
    return player == target
  end,
  on_refresh = function (self, event, target, player, data)
    for _, p in ipairs(player.room.players) do
      p:filterHandcards()
    end
    --覆写（临时举措，fix鏖战展示桃牌面，以及展示牌的合纵标记）
    function Room:showCards(cards, from)
      ---@cast self Room
      cards = Card:getIdList(cards)
      local src
      if from then src = from.id end
      self:sendLog{
        type = "#ShowCard",
        from = src,
        card = cards,
      }

      --[[

      self:doBroadcastNotify("ShowCard", {
        from = src,
        cards = cards,
      })
      self:sendFootnote(cards, {
        type = "##ShowCard",
        from = src,
      })
      
      ]]

      local UICards = table.map(cards, function(cid)
        return Fk:getCardById(cid)
      end)

      local n = 0
      if self.logic:getCurrentEvent().event == GameEvent.SkillEffect then
        n = self.logic:getCurrentEvent().id
      end

      self:showVirtualCard(UICards, from, {
        type = "##ShowCard",
        from = src,
      }, n)

      self.logic:trigger(fk.CardShown, from, { cardIds = cards })
    end
  end,
})

return battleRoyalVs
